/*
 * LinkedList.c
 *
 *  Created on: 07.01.2013
 *      Author: phru
 */

#include "LinkedList.h"

/**
 * Create the linked list
 */
linkedList_t* createLinkedList() {
	linkedList_t* newList = malloc(sizeof(linkedList_t*));
	newList->_listSize = 0;

	newList->set = set;
	newList->get = get;
	newList->add = add;
	newList->isEmpty = isEmpty;
	newList->size = size;
	newList->printList = printList;

	return newList;
}

/**
 * set the value at the position of a given node
 */
void set(linkedList_t* pList, uint32_t value) {

}

/**
 * returns the value of the given index
 */
int get(linkedList_t* pList, uint16_t index) {
	node_t* pCurrentNode = pList->_pFirstNode;
	if (pList->_listSize > 0) {
		int i;
		for (i = 0; i <= pList->_listSize; i++) {
			pCurrentNode = pCurrentNode->_pNext;
		}
	}
	return pCurrentNode->_value;
}

void add(linkedList_t* pList, uint32_t value) {
	node_t* pNewNode = createNode(value);

	/* first node */
	if (pList->_listSize == 0) {
		pList->_pFirstNode = pNewNode;
		pList->_pLastNode = pNewNode;
		pList->_listSize++;

		pNewNode->_pNext = NULL;
		pNewNode->_pPrev = NULL;
		pNewNode = NULL;
		return;

	} /*else if (pList->_listSize > 0) {*/

		pList->_pLastNode->_pNext = pNewNode;
		pNewNode->_pPrev = pList->_pLastNode;
		pList->_pLastNode = pNewNode;

		/*
		pNewNode->_pPrev = pList->_pLastNode;
		pNewNode->_pNext = NULL;
		pList->_pLastNode->_pNext = pNewNode;
		pList->_pLastNode = pNewNode;
		*/
		pList->_listSize++;

}

int isEmpty(linkedList_t* pList) {
	if(pList->_listSize == 0){
		return 0;
	}else {
		return 1;
	}
}

int size(linkedList_t* pList) {
	return pList->_listSize;
}

void printList(linkedList_t* pList) {
	int i = 0;
	node_t* pCurrentNode = pList->_pFirstNode;

	while(i < pList->_listSize){
		printf("Current node value on position %d is: %d \n",i++,pCurrentNode->_value);
		pCurrentNode = pCurrentNode->_pNext;
	}
}

node_t* createNode(uint32_t value) {
	node_t* newNode = malloc(sizeof(node_t*));
	newNode->_value = value;
	return newNode;
}

